package com.atmc.service.impl;

import com.atmc.entity.*;
import com.atmc.mapper.HeadPotraitMapper;
import com.atmc.mapper.UserMapper;
import com.atmc.service.UserService;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    @Qualifier("minioClient")
    MinioClient minioClient;
    @Value("${minio.bucketName}")
    private String bucketName;
    @Autowired
    HeadPotraitMapper headPotraitMapper;
    public User selectOne(String userName) {
        return userMapper.selectOne(userName);
    }

    @Override
    public User selectOneById(Long userId) {
        return userMapper.selectOneById(userId);
    }

    @Override
    public int insertOne(User user) {
        return userMapper.insertOne(user);
    }

    @Override
    public int updateOne(User user) {
        return userMapper.updateOne(user);
    }

    @Override
    public UserPotrait selectOneByName(String userName) {
        return userMapper.selectOneByName(userName);
    }

    @Override
    public List<UserPotrait> selectAllPotrait() {
        return userMapper.selectAllPotrait();
    }


    @Override
    public int updateUserSex(String userName,String sex) {
        return userMapper.updateUserSex(userName,sex);
    }

    @Override
    public ResponseResult updateUserInfo(String userName,
                                         String sex,
                                         MultipartFile file) {

        userMapper.updateUserSex(userName,sex);
        if (file == null || file .getSize()==0) {
            return new ResponseResult(400,"上传失败");
        }

        String originalFilename = file.getOriginalFilename();
        InputStream inputStream = null;
            try {
                boolean exists =
                        minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
                if (!exists) {
                    // 不存在，创建bucket
                    minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
                }
                inputStream = file.getInputStream();
                minioClient.putObject(
                        PutObjectArgs
                                .builder()
                                .bucket(bucketName)
                                .object(file .getOriginalFilename())
                                .stream(inputStream,file .getSize(),-1)
                                .contentType(file .getContentType())
                                .build());

            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                try {
                    if (inputStream !=null){
                        inputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            String fileUrls="http://43.143.140.144:9000/"+bucketName+"/"+originalFilename;
        Long userId = userMapper.selectOne(userName).getUserId();
        headPotraitMapper.updateOne(userId,fileUrls);
        String fileUrl = headPotraitMapper.selectOneUrl(userMapper.selectOneByName(userName).getUserId());
        String sex1 = userMapper.selectOne(userName).getSex();

        Map<Object,Object> userData=new HashMap<>();
        userData.put("userId",userId);
        userData.put("userName",userName);
        userData.put("sex",sex1);
        userData.put("fileUrl",fileUrl);
        return new ResponseResult(200,"修改成功",userData);
    }
}
